<?php


namespace app\api\controller;

use app\common\controller\Api;
use think\Exception;
use think\Db;

class Evaluate extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $needUserLogin = ['create', 'getUserEvaluate'];

    /**
     * 用户发起评价
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws Exception
     */
    public function create()
    {
        if ($this->request->isPost()) {
            $data = (array)$this->_data;
            $orderId = trim($this->request->post('order_id'));
            $evaluateData = $this->request->param('data');
            $evaluateData = html_entity_decode($evaluateData);
            $evaluateData = json_decode($evaluateData, true);

            $order = Db::table('fa_order')
                ->where('id', $orderId)
                ->where('user_id', $data['user_id'])
                ->where('status', 2)
                ->select();

            if (!$order) {

                $this->success('没有此订单', [], 201);
            }

            foreach ($evaluateData as $key => $val) {

                $serviceName = Db::table('fa_order')
                    ->where('id', $orderId)
                    ->value('service_name');

                Db::table('fa_evaluate')
                    ->insert(['user_id' => $data['user_id'],
                        'worker_id' => $val['worker_id'],
                        'order_id' => $orderId,
                        'service_name' => $serviceName,
                        'content' => $val['content'],
                        'score' => $val['score'],
                        'createtime' => time(),]);

                Db::table('fa_order')
                    ->where('id', $orderId)
                    ->update(['status' => 3]);

                $scoreAvg = Db::table('fa_evaluate')
                    ->where('worker_id', $val['worker_id'])
                    ->avg('score');

                Db::table('fa_worker')
                    ->where('id', $val['worker_id'])
                    ->update(['score' => $scoreAvg]);
            }

            $this->success('ok', [], 200);


        }


    }

    /**
     * 用获取用户的评价列表
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\db\exception\DataNotFoundException
     */
    public function getUserEvaluate()
    {
        if ($this->request->isPost()) {
            $data = (array)$this->_data;
            $page = trim($this->request->post('page'));

            $evaluateList = Db::table('fa_evaluate')
                ->alias('a')
                ->field('a.*,a.id,main_image,unit_price,area')
                ->join('fa_order b', 'a.order_id = b.id')
                ->where('a.user_id', $data['user_id'])
                ->order('a.createtime desc')
                ->paginate(5, null, ['page', $page]);

            $this->success('ok', ['evaluate' => $evaluateList->items(), 'total' => $evaluateList->total()], 200);
        }
    }
}